
const app = getApp()
import Toast from '@vant/weapp/toast/toast';
Page({
    data: {
      focus: false,
      strIccid: '',
      show:false,
      title:"是否绑定卡号",
      uId:'',
      userAvatar:'',
      userMobile:""
    },
    onLoad(options) {
        this.selectComponent("#loading").show()
        var that = this;
        app.getLogUser(function (uInfo) {
          const {
            userId,
            userAvatar,
            userMobile
          } = uInfo;
          that.setData({
            uId: userId,
            userAvatar:userAvatar,
            userMobile:userMobile
          })
        });
        this.selectComponent("#loading").hide()
      },
      //取消
    onCancel(){ },
    //扫码
    onScan(){
        const that = this;
        wx.scanCode({
          onlyFromCamera: true,
          success(res) {
            var scanResult = res.result;
            that.setData({strIccid:scanResult})
          }
        })
    },
    onConfirm(){
        const that = this
        const {strIccid}  = this.data
        wx.request({
            url:  app.globalData.apiUrl+'Mini/BalCard.ashx?action=11',
            data: {
              userId: that.data.uId,
              cardIccid:strIccid
            },
            dataType: 'json',
            header: {
              'content-type': 'application/json'
            },
            success(resroom) {
              var result = resroom.data;
              console.log(result)
              if (result.getNum == 0) {
                  //初次绑卡成功
                  Toast.success('绑卡成功');
                  wx.setStorageSync('cardId',result.getMsg);
                  wx.redirectTo({
                      url: '../service_choice/service_choice?cardId='+result.getMsg,
                  })
              } else if (result.getNum == 5) {
                // 添加 绑卡成功
                Toast.success('绑卡成功');
                wx.setStorageSync('cardId',result.getMsg);
                wx.redirectTo({
                  url: '../index/index?cardId='+result.getMsg,
                })
                // wx.redirectTo({
                //   url: '../service_choice/service_choice?cardId='+result.getMsg,
                // })
              } else {
                  //绑卡失败
                  Toast.fail(result.getMsg);
              }
            },
            fail(resfail) {
              console.log(resfail)
              Toast.fail(resfail.data);
            }
          })
    },
    //绑卡
    onBindCard:function(e){
       const {strIccid}  = this.data
       if(!strIccid){
        Toast.fail('请输入卡号');
       }else if(!(strIccid.length === 19 || strIccid.length === 10)){
        Toast.fail('卡号有误');
       } else{
         this.setData({ show: true });
       }
    },
    bindKeyInput: function (e) {
      this.setData({
        strIccid: e.detail.value
      })
    },
  })